
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.CoreGraphics;
using System.Drawing;
using MonoTouch.ObjCRuntime;
using System.IO;

namespace ComBrowser
{

	[Register("NavBookView")]
	public partial class NavBookView : UIView
	{

		private RectangleF rectView;
		CGContext context;

		float height = 432;
		float width = 288;
		float topOfGrid = 40;

		float numberOfColumns = 7;
		float numberOfRows = 10;
		float cellWidth = 0;
		float cellHeight = 0;

		int zeroBasedSelectedBookID=0;
		
		UIColor colorCurrentBook = UIColor.FromRGBA (0.4f, 0.2f, 0.7f, 0.5f);
		UIColor colorTitleShadow = UIColor.FromRGBA (106, 68, 33, 50);
		UIColor colorTouchDown = UIColor.FromRGBA (0.4f, 0.5f, 0.2f, 0.5f);
		
		NavBookViewController flipViewController;
		
		public NavBookView (NSCoder coder) : base(coder)
		{
		}

		public NavBookView (int SelectedBookID, NavBookViewController Ctrl)
		{
			float top = 0;
			float left = 0;
			this.BackgroundColor = UIColor.Clear;
			RectangleF me = new RectangleF (left, top, width, height);
			this.Frame = me;
			cellWidth = width / numberOfColumns;
			cellHeight = (height - topOfGrid) / numberOfRows;
			zeroBasedSelectedBookID = SelectedBookID;
			this.flipViewController = Ctrl;
			
		}
		public override void Draw (RectangleF rect)
		{
			AddBackgroundImage ();
			AddTitleLabel ();
			rectView = rect;
			
			context = UIGraphics.GetCurrentContext ();
			DrawInContext (context);
		}

		public void DrawInContext (CGContext ctx)
		{
			UIGraphics.PushContext (ctx);
			ctx.SetAllowsAntialiasing (true);
			AddGrid (ctx);
			AddChapterButtons ();
			AddNavButton ();
			UIGraphics.PopContext ();
		}

		void AddBackgroundImage ()
		{
			UIImage bck = UIImage.FromFileUncached(Path.Combine(NSBundle.MainBundle.BundlePath,@"Images/LargeBackgroundWithClose.png"));
			if(bck!=null)
			     UIGraphics.GetCurrentContext ().DrawImage (this.Frame, bck.CGImage);
		}

		void AddTitleLabel ()
		{
			UILabel l = new UILabel (new RectangleF (40, 7, 248, 22));
			l.TextColor = UIColor.White;
			l.Text = "Books Covered in " + ComDomain.Instance().Info.ShortName; 
			l.Font = UIFont.FromName ("Helvetica-Bold", 16);
			l.BackgroundColor = UIColor.Clear;
			l.ShadowColor = colorTitleShadow;
			l.ShadowOffset = new SizeF (2, 2);
			this.AddSubview (l);
		}

		void AddGrid (CGContext ctx)
		{
			ctx.SaveState ();
			
			float width = this.Frame.Width;
			//SizeF shadowSize = new SizeF (0f, -1f);
			//ctx.SetShadowWithColor (shadowSize, 2, new CGColor (0f, 0.7f));
			ctx.SetRGBStrokeColor (77 / 255, 20 / 255, 0, 0.5f);
			
			float heightOfGrid = this.Frame.Height;
			
			for (float i = cellWidth; i < width; i = i + cellWidth) {
				ctx.BeginPath ();
				ctx.MoveTo (i, topOfGrid);
				ctx.AddLineToPoint (i, heightOfGrid);
				ctx.SetLineWidth (0.5f);
				ctx.StrokePath ();
			}
			
			for (float i = cellHeight; i < height; i = i + cellHeight) {
				ctx.BeginPath ();
				ctx.MoveTo (0, i);
				ctx.AddLineToPoint (width, i);
				ctx.SetLineWidth (0.5f);
				ctx.StrokePath ();
			}
			
			//Draw Old/New split line
			ctx.BeginPath ();
			ctx.MoveTo (0, numberOfColumns * cellHeight);
			ctx.AddLineToPoint (width, numberOfColumns * cellHeight);
			ctx.SetLineWidth (5f);
			ctx.SetRGBStrokeColor (139 / 255, 64 / 255, 56 / 255, 0.5f);
			ctx.StrokePath ();
			ctx.RestoreState ();
		}

		void AddChapterButtons ()
		{
			BookCollection books = BookCollection.Instance ();
			
			int rowNumber = 1;
			int columnNumber = 0;
			ComDomain domain = ComDomain.Instance ();
			
			foreach (Book book in books) {
				float Y = rowNumber * cellHeight - 2;
				float X = columnNumber * cellWidth - 2;
				
				RectangleF frame = new RectangleF (X + 3, Y + 3, cellWidth - 2, cellHeight - 2);
				
				if (domain.OneBasedBookIDs.Contains (book.OneBasedBookID))
				{
					UIButton button = CreateButton (frame, book);
					if (book.ZeroBasedBookID == this.zeroBasedSelectedBookID)
						button.BackgroundColor = colorCurrentBook;
					this.AddSubview (button);
				}
				
				columnNumber++;
				
				if (columnNumber == numberOfColumns) {
					columnNumber = 0;
					rowNumber++;
				}
				
				//Start a new line when we're at the new testament
				if (book.OneBasedBookID == 39) {
					columnNumber = 0;
					rowNumber = 7;
				}
			}
		}
		
		void AddNavButton ()
		{
			UIButton button = new UIButton (new RectangleF (1, 1, 50, 50));
			button.BackgroundColor = UIColor.Clear;
			button.TouchUpInside += (s, e) =>
			{
				this.flipViewController.CancelView ();
			};
			this.AddSubview (button);
			 
		}

		UIButton CreateButton (RectangleF frame, Book zeroBasedBookID)
		{
			UIButton button = new UIButton (frame);
			button.SetTitle (zeroBasedBookID.Short, UIControlState.Normal);
			button.SetTitleColor (zeroBasedBookID.Color, UIControlState.Normal);
			button.TitleLabel.Font = UIFont.FromName ("Helvetica", 14);
			button.BackgroundColor = UIColor.Clear;
			button.Tag = zeroBasedBookID.ZeroBasedBookID;
			button.TouchDown += (s, e) => ((UIButton)s).BackgroundColor = colorTouchDown;
			button.TouchUpInside += (s, e) =>
			{
				((UIButton)s).BackgroundColor = UIColor.Clear;
				this.flipViewController.SwitchToChapterView (button.Tag);
			};
			button.TouchUpOutside += (s, e) => ((UIButton)s).BackgroundColor = UIColor.Clear;
			return button;
		}

		
		
	}
	
	 
}
